<template>
  <div class="learning-overview">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>学习记录总览（按学生统计）</span>
          <el-button type="primary" @click="loadOverview">刷新统计</el-button>
        </div>
      </template>

      <el-table :data="items" stripe style="width: 100%" v-loading="loading">
        <el-table-column prop="studentName" label="学生姓名" width="150" />
        <el-table-column prop="studentNo" label="学号" width="140" />
        <el-table-column prop="totalRecords" label="记录数" width="100" />
        <el-table-column prop="totalDurationMinutes" label="总时长(分钟)" width="140" />
        <el-table-column prop="avgDurationMinutes" label="平均时长(分钟)" width="160">
          <template #default="{ row }">
            {{ Number(row.avgDurationMinutes || 0).toFixed(1) }}
          </template>
        </el-table-column>
        <el-table-column prop="coursesCount" label="课程数量" width="120" />
        <el-table-column prop="earliestStart" label="最早起始" width="180" />
        <el-table-column prop="latestStart" label="最新起始" width="180" />
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { learningRecordApi } from '@/api/learningRecord'

export default {
  name: 'LearningRecordOverview',
  setup() {
    const loading = ref(false)
    const items = ref([])

    const loadOverview = async () => {
      loading.value = true
      try {
        const res = await learningRecordApi.getOverview()
        items.value = Array.isArray(res.data?.data) ? res.data.data : []
      } catch (e) {
        ElMessage.error(e?.response?.data?.message || e.message || '加载统计失败')
      } finally {
        loading.value = false
      }
    }

    onMounted(() => {
      loadOverview()
    })

    return {
      loading,
      items,
      loadOverview
    }
  }
}
</script>

<style scoped>
.learning-overview { padding: 20px; }
.card-header { display: flex; justify-content: space-between; align-items: center; }
</style>